{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Intro\n",
    "Notebook that explores image processing, preparation and augmentation with Keras\n",
    "\n",
    "## Resources and Links\n",
    "* http://machinelearningmastery.com/image-augmentation-deep-learning-keras/\n",
    "* https://keras.io/preprocessing/image/\n",
    "* https://www.kaggle.com/hexietufts/ultrasound-nerve-segmentation/easy-to-use-keras-imagedatagenerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:00:22.942938",
     "start_time": "2017-08-02T19:00:12.313330Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from keras.preprocessing import image\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import numpy as np\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:00:22.948938",
     "start_time": "2017-08-02T19:00:22.944938Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "imgs_dir = \"\"\n",
    "img_size = (1024,1024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def get_batches(path, gen=image.ImageDataGenerator(dim_ordering=\"th\"), shuffle=True, batch_size=8, class_mode='categorical'):\n",
    "    return gen.flow_from_directory(path, target_size=img_size, class_mode=class_mode, \n",
    "                                   shuffle=shuffle, batch_size=batch_size, color_mode='grayscale')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:00:22.961939",
     "start_time": "2017-08-02T19:00:22.950938Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load_image(filepath):\n",
    "    img = cv2.imread(filepath, cv2.IMREAD_COLOR)\n",
    "    return img\n",
    "\n",
    "def load_data(images, img_size):\n",
    "    data = np.array([load_image(img, img_size).T for img in images])\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:00:23.539972",
     "start_time": "2017-08-02T19:00:23.514970Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "img = load_image(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:00:30.886392",
     "start_time": "2017-08-02T19:00:30.872391Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "img.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:00:50.158494",
     "start_time": "2017-08-02T19:00:50.150494Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "img.reshape(1, *img.shape).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-02T19:01:04.840334",
     "start_time": "2017-08-02T19:01:04.825333Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "np.array([img]).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "X_batch, y_batch = next(get_batches(imgs_dir))\n",
    "print(X_batch[0].shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-06-01T12:32:03.911056",
     "start_time": "2017-06-01T12:32:03.898055Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "shift = 0.1\n",
    "rotation_range = 90\n",
    "zoom_range = 0.1\n",
    "shear_range = 0.1\n",
    "channel_shift_range = 20\n",
    "horizontal_flip = True\n",
    "vertical_flip = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-06-01T12:32:04.282077",
     "start_time": "2017-06-01T12:32:04.275077Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "imgen = image.ImageDataGenerator(rotation_range=rotation_range, \n",
    "                                 width_shift_range=shift, height_shift_range=shift,\n",
    "                                zoom_range=zoom_range, shear_range=shear_range, channel_shift_range=channel_shift_range,\n",
    "                                horizontal_flip=horizontal_flip, vertical_flip=vertical_flip)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-06-01T12:34:42.702138",
     "start_time": "2017-06-01T12:34:36.813801Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(2):\n",
    "    res = next(imgen.flow_from_directory(imgs_dir, target_size=img_size, classes=['car_ok'], class_mode='categorical', batch_size=10, \n",
    "                              save_to_dir=imgs_dir))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "X_batch, y_batch = next(get_batches(imgs_dir, imgen))\n",
    "print(X_batch[0].shape)\n",
    "fig, axes = plt.subplots(len(X_batch))\n",
    "for i, x in enumerate(X_batch):\n",
    "    axes[i].imshow(x.reshape(img_size), cmap=ptl.get_cmap('gray'))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Scikit-Image Transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-23T16:42:04.052749",
     "start_time": "2017-07-23T16:42:04.044748Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from skimage import data\n",
    "from skimage import transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-23T16:43:04.100183",
     "start_time": "2017-07-23T16:43:03.909173Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "image = data.camera()\n",
    "plt.imshow(image)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-23T16:51:47.257106",
     "start_time": "2017-07-23T16:51:47.038094Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.imshow(transform.rotate(image, 30, mode='edge'))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:neural-networks]",
   "language": "python",
   "name": "conda-env-neural-networks-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "49px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
